package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.adapter.ShoplistAdapter;
import com.dongdongsongcan.app.control.FavouriteControl;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshListView;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

public class FavouriteListActivity extends BaseActivity {
    PullToRefreshListView mShoplist;
    ShoplistAdapter mShoplistAdapter;
    private int mOffset;
    private int PAGE_SIZE = 10;


    public static void startFrom(Context context) {
        Bundle bundle = new Bundle();
        DpActivityManager.i().startActivity(context, FavouriteListActivity.class, bundle);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoplist);
        setTitle("商铺收藏列表");
    }

    @Override
    public void initView() {
        mShoplist = (PullToRefreshListView) findViewById(R.id.shop_list);
        mShoplistAdapter = new ShoplistAdapter(this);
        mShoplist.setAdapter(mShoplistAdapter);
    }

    @Override
    public void setListener() {
        mShoplist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mOffset = 0;
                mShoplistAdapter.clear();
                getShops();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getShops();
            }
        });
        mShoplist.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopDetailActivity.startFrom(FavouriteListActivity.this, mShoplistAdapter.getData(position - 1));
            }
        });
        mShoplist.getRefreshableView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                TipsUtil.showConfirmDialog(FavouriteListActivity.this, "确定", "确定删除吗?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FavouriteControl.i().user_collect_del(mShoplistAdapter.getData(position - 1).getShop_id(), new CtrlTask.IControlResponse<ShopData>() {
                            @Override
                            public void onControlResponse(ShopData data) {
                                TipsUtil.toast(data.getMsg());
                                getShops();
                            }
                        });
                    }
                });

                return true;
            }
        });
    }

    @Override
    public void initData() {
        getShops();
    }

    private void getShops() {
        FavouriteControl.i().user_collect(
                new CtrlTask.IControlResponse<ShopData>() {

                    @Override
                    public void onControlResponse(ShopData data) {
                        mShoplistAdapter.setData(data.getResult());
                        mShoplist.onRefreshComplete();

                    }
                });
    }

}
